package com.blyat.xsoft.kernel.restful.pagination;

/**
 * @Auther: syh
 * @Date: 2020/6/9
 * @Description:
 */
public class Page {
    private Integer DEFAULT_PAGE = 0;
    private Integer DEFAULT_PAGE_SIZE = 10;
    private Integer page;
    private Integer pageSize;
    private Integer total;

    public Page() {
        this.page = this.DEFAULT_PAGE;
        this.pageSize = this.DEFAULT_PAGE_SIZE;
    }

    public Page(Integer page, Integer pageSize) {
        this.page = this.DEFAULT_PAGE;
        this.pageSize = this.DEFAULT_PAGE_SIZE;
        this.page = page;
        this.pageSize = pageSize;
    }

    public Integer getPage() {
        if (this.page == null || this.page < 0) {
            this.page = this.DEFAULT_PAGE;
        }

        return this.page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        if (this.pageSize == null || this.pageSize <1) {
            this.pageSize = this.DEFAULT_PAGE_SIZE;
        }

        return this.pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotal() {
        return this.total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getFirstResult() {
        Integer firstResult = (this.getPage() - 1) * this.getPageSize();
        if (firstResult >= this.getTotal()) {
            firstResult = 0;
        }

        return firstResult;
    }

    public Integer getMaxResults() {
        return this.getPageSize();
    }
}
